package com.viknix.threadpool.manager.http.handler;

import com.viknix.threadpool.manager.common.util.IOUtils;
import com.viknix.threadpool.manager.http.Header;
import com.viknix.threadpool.manager.http.response.HttpClientResponse;
import com.viknix.threadpool.manager.http.result.HttpRestResult;

import java.lang.reflect.Type;

/**
 * @Author: Dongqi
 * @Date: 2021/11/22 10:57
 * @Version 1.0
 * @Description: string response handler, Mainly converter response type as string type.
 */
public class StringResponseHandler extends AbstractResponseHandler<String> {

    @Override
    public HttpRestResult<String> convertResult(HttpClientResponse response, Type responseType) throws Exception {
        final Header headers = response.getHeaders();
        String extractBody = IOUtils.toString(response.getBody(), headers.getCharset());
        return new HttpRestResult<String>(headers, response.getStatusCode(), extractBody, null);
    }
}
